//
//  ViewController.m
//  HYHoney
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "NetworkHandeler.h"    
#import "NetworkingHandle.h"   // Block
#import "Model.h"
#import "ModelOfNR.h"
#import "Cell.h"
#import "CellOfNR.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

#import "HeadScrollView.h"
#import "PushTopWebview.h"
#import "PushForSearch.h"

#import "PushForHYNR.h"
#import "ModelForPush.h"
#import "DKNightVersion.h"

@interface ViewController ()<NetworkHandelerDelegate, UITableViewDelegate, UITableViewDataSource, HeadScrollViewDelegate, UIScrollViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, assign)BOOL isRefreshing;   // 刷新状态
@property (nonatomic, assign)BOOL isRef;

@property (nonatomic, retain)NSMutableString *string;
@property (nonatomic, retain)NSMutableArray *arrTJ;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    self.navigationItem.title = @"Honey Home";
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1 green:1 blue:1 alpha:1], NSForegroundColorAttributeName, nil]];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xial.png"] style:UIBarButtonItemStylePlain target:self action:@selector(presentLeftMenuViewController:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightMenuForSearch:)];
    
    
    
    
    [self handelData];
    [self handelDataNR];
    [self createTableView];
}


- (void)createTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[Cell class] forCellReuseIdentifier:@"pool1"];
    
    [self.tableView registerClass:[CellOfNR class] forCellReuseIdentifier:@"pool2"];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 60, 0);
    
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xFFFAFA, 0x27408B);
    
    
    // 实现刷新功能
    self.isRefreshing = NO;
    self.isRef = NO;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        // 如果进入刷新状态, 即自动调用此块代码.
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableView.mj_header endRefreshing];
            
            self.isRefreshing = YES;
            self.isRef = YES;
        });
    }];
    
    
    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.mj_footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.tableView.mj_footer endRefreshing];
            
            self.isRefreshing = YES;
            
        });
        
    }];
    
    [self.tableView.mj_footer beginRefreshing];
    
    [self.tableView release];
}



// Top

- (void)handelData{
    
    NSString *str = @"http://api.homer.app887.com/api/Articles.action" ;
    
    [NetworkHandeler handelerJSONWithURL:str delegate:self];
    
}


- (void)handelerDidComplete:(id)result{

    
    self.arr = [NSMutableArray array];
    
    NSDictionary *dic = [result valueForKey:@"root"];
    
    NSArray *arr = [dic objectForKey:@"list"];
        
        for (NSDictionary *dicRes in arr) {
            
            NSString *str = [dicRes valueForKey:@"imglink"];
            
            if (![str hasSuffix:@"gif"]) {
                
                Model *model = [[Model alloc] init];
                
                model.imglink = [dicRes objectForKey:@"imglink"];
                model.title = [dicRes objectForKey:@"title"];
                model.url = [dicRes objectForKey:@"url"];
                
                [self.arr addObject:model];
                
                [model release];
            }
        }
        
    [self.tableView reloadData];
}


- (void)viewWillAppear:(BOOL)animated {
    
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];
    
}

// other Cell
- (void)handelDataNR{

    NSString *str = @"http://api.bohejiaju.com/v1/collections?limit=20&offset=0";

    [NetworkingHandle networkingWithURL:str completeHandel:^(NSURLResponse *response, NSData *data, id result) {
        
        self.arrTJ = [NSMutableArray array];
        
        NSDictionary *dicData = [result valueForKey:@"data"];
        
        NSArray *arr = [dicData valueForKey:@"collections"];
        
        for (NSDictionary *dic in arr) {
            
            ModelOfNR *model = [[ModelOfNR alloc] init];
        
            model.cover_image_url = [dic objectForKey:@"cover_image_url"];
            model.idN = [dic objectForKey:@"id"];
            model.title = [dic objectForKey:@"title"];
            [self.arrTJ addObject:model];
            
            [model release];
        }
        
        [self.tableView reloadData];
    }];
    
}


- (void)rightMenuForSearch:(UIBarButtonItem *)btn{

    PushForSearch *search = [[PushForSearch alloc] init];
    
    [self.navigationController pushViewController:search animated:YES];
}




// 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PushForHYNR *push = [[PushForHYNR alloc] init];
    
    ModelOfNR *model = [self.arrTJ objectAtIndex:indexPath.row - 1];
    
    push.idN = [model.idN integerValue];
    
    [self.navigationController pushViewController:push animated:YES];
    
}



- (void)clickImageView:(HeadScrollView *)imageView{
    
    PushTopWebview *push = [[PushTopWebview alloc] init];
    
//    NSInteger i = imageView.tag - 1;
//    Model *model = [self.arr objectAtIndex:i];
//    
//    [push passModel:model];
    [self.navigationController pushViewController:push animated:NO];
}



- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrTJ.count + 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row == 0) {
    
        Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell passModel:self.arr];
        
        return cell;
        
    }else{
    
        CellOfNR *cellI = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        
        ModelOfNR *modelNR = [self.arrTJ objectAtIndex:indexPath.row - 1];
        cellI.selectionStyle = UITableViewCellSelectionStyleNone;
        [cellI passmodel:modelNR];
        
        return cellI;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        
        return 190;
        
    }else
        
        return 220;
}



- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
}



- (void)dealloc{

    [_string release];
    [_arr release];
    [_arrTJ release];
    [_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
